package com.example.controller.auth;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.result.Result;
import com.example.entity.auth.SysPost;
import com.example.entity.vo.auth.SysPostQueryVo;
import com.example.service.auth.SysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : ssy
 * @date: 2024-03-26
 * @Description:
 */
@Api(tags = "岗位管理接口")
@RestController
@RequestMapping("/api/system/sysPost")
public class SysPostController {

    @Autowired
    private SysPostService sysPostService;

    @GetMapping("/get/{id}")
    @ApiOperation("根据id获取岗位")
    public Result<SysPost> getPostById(@PathVariable Long id){
        SysPost byId = sysPostService.getById(id);
        return Result.success(byId);
    }

    @GetMapping("{page}/{limit}")
    @ApiOperation("岗位条件分页查询")
    public Result<IPage<SysPost>> getAll(@PathVariable Long page,
                                         @PathVariable Long limit,
                                         SysPostQueryVo sysPostQueryVo){
        Page<SysPost> param = new Page<>(page,limit);
        LambdaQueryWrapper<SysPost> wrapper = new LambdaQueryWrapper<>();
        String postCode = sysPostQueryVo.getPostCode();
        Boolean status = sysPostQueryVo.getStatus();
        String name = sysPostQueryVo.getName();
        if (!StringUtils.isEmpty(name)){
            wrapper.like(SysPost::getName,name);
        }
        if (sysPostQueryVo.getStatus()!=null){
            wrapper.eq(SysPost::getStatus,status);
        }
        if (!StringUtils.isEmpty(postCode)){
            wrapper.like(SysPost::getPostCode,postCode);
        }
        IPage<SysPost> sysPostIPage = sysPostService.page(param,wrapper);
        return Result.success(sysPostIPage);
    }

    @ApiOperation("添加岗位")
    @PostMapping("save")
    public Result save(@RequestBody SysPost sysPost){
        boolean save = sysPostService.save(sysPost);
        if (!save){
            return Result.error("新增岗位失败");
        }
        return Result.success("新增岗位成功");
    }

    @ApiOperation("修改岗位")
    @PostMapping("update")
    public Result update(@RequestBody SysPost sysPost){
        boolean update = sysPostService.updateById(sysPost);
        if (!update){
            return Result.error("修改岗位失败");
        }
        return Result.success("修改岗位成功");
    }

    @ApiOperation("根据id删除岗位")
    @PostMapping("remove/{id}")
    public Result remove(@PathVariable("id") Long id){
        boolean update = sysPostService.removeById(id);
        if (!update){
            return Result.error("删除岗位失败");
        }
        return Result.success("删除岗位成功");
    }

    //批量删除
    // 前端数组 [1,2,3]
    @ApiOperation("批量删除")
    @PostMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean batchRemove = sysPostService.removeByIds(idList);
        if (!batchRemove){
            return Result.error("删除失败");
        }
        return Result.success("删除岗位成功");
    }
}
